<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<!-- bootstrap样式、主题、核心文档 -->
<link rel="stylesheet" href="../libs/bootstrap-3.3.7-dist/bootstrap-3.3.7-dist/css/bootstrap.min.css" />
<link rel="stylesheet" href="../libs/bootstrap-3.3.7-dist/bootstrap-3.3.7-dist/css/bootstrap-theme.min.css" />
<script type="text/javascript" src="../libs/bootstrap-3.3.7-dist/bootstrap-3.3.7-dist/js/bootstrap.min.js"></script>
<script type="text/javascript" src="../libs/jquery/jquery.js"></script>
<style type="text/css">
.table > tbody > tr > td, .table > tbody > tr > th, .table > tfoot > tr > td, .table > tfoot > tr > th, .table > thead > tr > td, .table > thead > tr > th {

    padding: 8px;
    line-height: 1.42857143;
    vertical-align: middle;
    border-top: 1px solid #ddd;

}

	tbody>tr>td{
		width:134px;
		text-align: center;
		padding:0px;
		vertical-align: middle;
	}
	th{
	width:120px;
	text-align: center;
	vertical-align: middle;}
</style>
</head>
<body style="background-color: Azure">
	<nav class="navbar navbar-default navbar-fixed-top">
	    <table style="width:100%; height: 40px; ">
	    	<thead>
				<tr>
				<th>消费类型</th>
				<th>借款金额</th>
				<th>借款时长</th>
				<th>起始时间</th>
				<th>截止时间</th>
				<th>月利率</th>
				<th>预期总收益(按出借比例分配)</th>
				<th>信誉等级</th>
				<th>出借金额(整百计)</th>
				<th>操作</th>
				</tr>
			</thead>
	    </table>
	</nav>
	<div class="jumbotron" style="background-color:Azure">
		<table class="table table-hover">
			<tbody>
			</tbody>
		</table>
	</div>
	<script type="text/javascript">
	$(function(){
		
		//消费类型、借款金额、时长、起始时间、截止时间，月利息、信誉等级、输入你的借款金额、操作.隐藏借款人信息
		$.ajax({
			type:"post",
			url:"../LoanbillInfoServlet",
			dataType:"json",
			success:function(data){
				$("tbody").empty();
				$.each(data,function(index,lb){
					var pe=lb.money*lb.monthrate*lb.duration;
					pe=pe.toFixed(2);
					var monthrate=lb.monthrate*100;
					monthrate=monthrate.toFixed(2);
					var residue=lb.money-lb.lendmoney;
					var percent=lb.lendmoney/lb.money*100;
					$("tbody").append("<tr id='tr"+index+"'><td>"+lb.name+"</td><td>"+lb.money+"<div class='progress' style='height:10px'><div class='progress-bar' role='progressbar' aria-valuemin='0' aria-valuemax='100' ></div></div></td><td>"+lb.duration+"</td><td>"+lb.starttime+"</td><td>"+lb.endtime+"</td><td>"+monthrate+"%</td><td>"+pe+"</td><td>"+lb.creditname+"</td><td><input type='text' name='lendmoney' placeholder='"+residue.toFixed(2)+"' class='form-control'><input type='hidden' name='loanbillid' value='"+lb.id+"'></td><td><button class='btn btn-info' name='"+index+"'>出借</button></td></tr>");
					$("#tr"+index).find("div[class=progress-bar]").css({"width":percent+"%","min-width":"2em","line-height":"12px"}).html(percent+"%");
				});
			}
		})
		
		//给出借按钮增加时间
		$("button").live('click',function(){
			var namevalue=$(this).attr("name");
			var lbid=$("#tr"+namevalue).find("input").eq(1).val();//借款单id
			var money = $("#tr"+namevalue).find("input").eq(0).val();//输入的金额
			var lenderid = "${user.id}";//出借人id
			var residue = $("#tr"+namevalue).find("input").eq(0).attr("placeholder");
			if(money==""){
				alert("出借金额不能为空");
			}else{
				if(Number(money)>Number(residue)){
					alert("超出出借上限");
				}else{
					//如果出借金额=出借上限
					if(Number(money)==Number(residue)){
						alert("进入等额记录");
						$.post("../BalanceMinusServlet",{userid:lenderid,money:money},function(result){
							if(result.flag){
								//把出借记录写入lendbill表
								$.ajax({
									type:"post",
									url:"../LendbillAddServlet",
									data:"loanbillid="+lbid+"&money="+money+"&lenderid="+lenderid,
									dataType:"json",
									success:function(result2){
										if(result2.flag){
											//修改借款单信息
											$.post("../LoanbillUpdateServlet",{id:lbid},function(result3){
												if(result3.flag){
													//增加还款单信息
													$.post("../RefundbillAddServlet",{id:lbid},function(result4){
														if(result4.flag){
															alert("出借"+result.message);
															location.reload();
														}else{
															alert("出借"+result.message);
														}
													},"json");
												}else{
													alert("出借"+result.message);
												}
											},"json");
										}else{
											alert("出借"+result.message);
										}
									}
								});
							}else{
								alert("出借"+result.message);
							}
							
						},"json");
					}else{
						//先从余额中减掉出借金额，操作的余额表
						$.post("../BalanceMinusServlet",{userid:lenderid,money:money},function(result){
							if(result.flag){
								//把出借记录写入lendbill表
								$.ajax({
									type:"post",
									url:"../LendbillAddServlet",
									data:"loanbillid="+lbid+"&money="+money+"&lenderid="+lenderid,
									dataType:"json",
									success:function(result2){
										if(result2.flag){
											alert("出借"+result.message);
											location.reload();
										}else{
											alert("出借"+result.message);
										}
									}
								});
							}else{
								alert("出借"+result.message);
							}
							
						},"json");
					}
				}
			}
		});
	});
</script>
</body>
</html>